package icbc.app
{
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import icbc.app.control.Alert;
	import icbc.app.event.AppEvent;
	import icbc.app.setting.event.ReaderEvent;
	import icbc.app.setting.reader.MenuReader;
	import icbc.app.util.logger.Log;

	public class AppGlobal extends EventDispatcher
	{
		public static const VERSION:String = "1.1.6";
		public static const DEFAULT_WIDTH:uint = 776;
		public static const DEFAULT_HEIGHT:uint = 443;
		public static const FONT_NAME:String = "Arial";
		public static const FONT_SIZE:uint = 12;
		public static const FONT_SMALL_SIZE:uint = 10;
		public static const FONT_COLOR:uint = 0x333333;
		public static const RED_ICBC:uint = 0xc3171c;
		public static var PathConfig:String = "icbcdata/config.xml";
		public static var ROOT_PATH:String = "";
		public static var PATH_MENU:String = null;
		public static var PATH_BACKGROUND:String = null;
		public static var PATH_FRONT_IMAGE:String = null;
		public static var FRONT_TIME_OUT:int = 5;
		public static var CONTENT_TEXT_LEADING:int = 5;
		public static var BLINK_TIME_OUT:uint = 15;

		public function init(stage:Stage, loadConfig:Boolean = true):void
		{
			Log.Info("Global.version > " + VERSION);
			stage.stageFocusRect = false;
			Alert.init(stage);
			initAppConfig();
		}
		
		private function initAppConfig():void
		{
			var config:AppConfig = new AppConfig();
			config.addEventListener(AppEvent.CONFIG_READY, appConfigReady);
			config.init();
		}

		private function appConfigReady(evt:Event):void
		{
			(evt.target as AppConfig).removeEventListener(AppEvent.CONFIG_READY, appConfigReady);
			loadMenu();
		}

		public function loadMenu():void
		{
			var reader:MenuReader = new MenuReader(combinePath(PATH_MENU));
			reader.addEventListener(ReaderEvent.READER_READY, menuReady);
			reader.init();
		}

		private function menuReady(evt:ReaderEvent):void
		{
			(evt.target as MenuReader).removeEventListener(ReaderEvent.READER_READY, menuReady);
			globalReady();
		}

		private function globalReady():void
		{
			dispatchEvent(new AppEvent(AppEvent.GLOBAL_READY));
		}
		
		public static function combinePath(path:String):String
		{
			return ROOT_PATH+ "/" +path;
		}
	}
}